package de.pyranja.storage.support;

import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.pyranja.storage.core.Archive;
import de.pyranja.storage.exception.BinaryStorageException;

/**
 * An action in the binary storage that will result in creating a reference on
 * a blob id if it exists.
 * @author Chris Borckholder
 */
public class ClaimAction implements Callable<String> {
	/* slf4j-logger */
	private final static Logger log = LoggerFactory.getLogger(ClaimAction.class);
	/* parameters */
	private final String id;
	private final Archive archive;
	
	public ClaimAction(final String id, final Archive archive) {
		this.id = id;
		this.archive = archive;
	}

	/**
	 * Executes this action.
	 * @return id of targeted blob
	 */
	@Override
	public String call() throws BinaryStorageException {
		log.info("CLAIMING {}", id);
		int references = archive.addIfExisting(id);
		log.debug("claiming succeeded - {} is now referenced {} times", id, references);
		return id;
	}
}